package com.example.its2021606134chh.MyMessageModule;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Spinner;
import android.widget.Toast;

import androidx.fragment.app.Fragment;

import com.example.its2021606134chh.HttpUtil;
import com.example.its2021606134chh.R;
import com.example.its2021606134chh.ThresholdModule.ThresholdParams;
import com.google.gson.Gson;

import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link MM_queryFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class MM_queryFragment extends Fragment {

    // TODO: Rename parameter arguments, choose names that match
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
    private static final String ARG_PARAM1 = "title";
    private static final String ARG_PARAM2 = "param2";

    // TODO: Rename and change types of parameters
    private String mParam1;
    private String mParam2;
    private Spinner spinner;
    private ListView listview;
    List<MessageParams> list = new ArrayList<MessageParams>();
    List<MessageParams> listTemperature = new ArrayList<MessageParams>();
    List<MessageParams> listHumidity = new ArrayList<MessageParams>();
    List<MessageParams> listLightIntensity = new ArrayList<MessageParams>();
    List<MessageParams> listCo2 = new ArrayList<MessageParams>();
    List<MessageParams> listPm25 = new ArrayList<MessageParams>();

    public MM_queryFragment() {
        // Required empty public constructor
    }

    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     *
     * @param param1 Parameter 1.
     * @param param2 Parameter 2.
     * @return A new instance of fragment MM_queryFragment.
     */
    // TODO: Rename and change types and number of parameters
    public static MM_queryFragment newInstance(String param1, String param2) {
        MM_queryFragment fragment = new MM_queryFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_m_m_query, container, false);
        spinner = (Spinner) view.findViewById(R.id.spinner);
        listview = (ListView) view.findViewById(R.id.listview);
        View listView = LayoutInflater.from(getContext()).inflate(R.layout.mm_listview_heard, null);
        listview.addHeaderView(listView);
        ListViewAdapter adapter = new ListViewAdapter(list, getContext());
        listview.setAdapter(adapter);

        String[] array = new String[] {"全部", "温度", "湿度", "光照", "Co2", "PM2.5"};
        ArrayAdapter<String> spinnerAdapter = new ArrayAdapter<String>(getContext(), android.R.layout.simple_spinner_item, array);
        spinner.setAdapter(spinnerAdapter);
        spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                switch (position) {
                    case 0:
                        adapter.setList(list);
                        break;
                    case 1:
                        adapter.setList(listTemperature);
                        break;
                    case 2:
                        adapter.setList(listHumidity);
                        break;
                    case 3:
                        adapter.setList(listLightIntensity);
                        break;
                    case 4:
                        adapter.setList(listCo2);
                        break;
                    case 5:
                        adapter.setList(listPm25);
                        break;
                    default:
                        adapter.setList(new ArrayList<MessageParams>());
                }
                adapter.notifyDataSetChanged();
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });

        Timer timer = new Timer();
        TimerTask timerTask = new TimerTask() {
            @Override
            public void run() {
                HttpUtil.asyncRequest(new HttpUtil.AsyncCallBack() {
                    @Override
                    public void onFailure(Exception e) {

                    }

                    @Override
                    public void onResponse(String json) {
                        Gson gson = new Gson();
                        ThresholdParams params = gson.fromJson(json, ThresholdParams.class);
                        int Temperature = Integer.parseInt(params.getTemperature());
                        int Humidity = Integer.parseInt(params.getHumidity());
                        int LightIntensity = Integer.parseInt(params.getLightIntensity());
                        int Co2 = Integer.parseInt(params.getCo2());
                        int Pm25 = Integer.parseInt(params.getPm25());
                        if (Temperature > 20) {
                            MessageParams messageParams = new MessageParams();
                            messageParams.setType("【温度】报警");
                            messageParams.setThreshold("20");
                            messageParams.setValues(String.valueOf(Temperature));
                            list.add(messageParams);
                            listTemperature.add(messageParams);
                        }
                        if (Humidity > 80) {
                            MessageParams messageParams = new MessageParams();
                            messageParams.setType("【湿度】报警");
                            messageParams.setThreshold("80");
                            messageParams.setValues(String.valueOf(Humidity));
                            list.add(messageParams);
                            listHumidity.add(messageParams);
                        }

                        if (LightIntensity > 2000) {
                            MessageParams messageParams = new MessageParams();
                            messageParams.setType("【光照】报警");
                            messageParams.setThreshold("2000");
                            messageParams.setValues(String.valueOf(LightIntensity));
                            list.add(messageParams);
                            listLightIntensity.add(messageParams);
                        }
                        if (Co2 > 3000) {
                            MessageParams messageParams = new MessageParams();
                            messageParams.setType("【Co2】报警");
                            messageParams.setThreshold("3000");
                            messageParams.setValues(String.valueOf(Co2));
                            list.add(messageParams);
                            listCo2.add(messageParams);
                        }
                        if (Pm25 > 80) {
                            MessageParams messageParams = new MessageParams();
                            messageParams.setType("【PM2.5】报警");
                            messageParams.setThreshold("80");
                            messageParams.setValues(String.valueOf(Pm25));
                            list.add(messageParams);
                            listPm25.add(messageParams);
                        }
                        adapter.notifyDataSetChanged();
                    }
                }, "get_all_sense", new String[]{"UserName"}, "user1");
            }
        };
        timer.schedule(timerTask, 0, 3000);
        // Inflate the layout for this fragment

        return view;
    }
}